#include "lv_user_ui.h"
#include "lv_user.h"
#include "app.h"
#include "io.h"

// .0123456789qwertyuiop[]{}\|asdfghjkl;':"zxcvbnm,./<>?!@#$%^&*()_+-=QWERTYUIOPASDFGHJKLZXCVBNM
//电压流温度时间功率池运行背光亮设置模式放大倍数阻源风扇开关转速保护散热

static void lv_task_main_handler(lv_timer_t * timer);

ui_status_page ui_status;

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_group_t * g;
static lv_timer_t * lv_task_main;

extern __IO system_obj sys_cur_para;
extern __IO uint16_t adc_avg_arr[];
extern void dac_handler(uint16_t para);

static void lv_task_main_handler(lv_timer_t * timer)
{
    sys_cur_para.input_vol = (adc_avg_arr[0] * 3300 * 48 / 4096);
    sys_cur_para.input_cur = (adc_avg_arr[1] * 22000 / 4096);
    sys_cur_para.output_vol = (adc_avg_arr[2] * 3300 * 48 / 4096);
    sys_cur_para.output_cur = (adc_avg_arr[3] * 22000 / 4096);
    //运行时间
    lv_label_set_text_fmt(ui_status.label_run_time,"Time:%d:%02d",(lv_tick_get()/60000),(lv_tick_get()/1000)%60);
    
    lv_label_set_text_fmt(ui_status.para_obj[0].label_para[0], "%3d.%02d", sys_cur_para.input_vol / 1000, (sys_cur_para.input_vol / 10) % 100);
    lv_label_set_text_fmt(ui_status.para_obj[0].label_para[1], "%3d.%02d", sys_cur_para.input_cur / 1000, (sys_cur_para.input_cur / 10) % 100);
    lv_label_set_text_fmt(ui_status.para_obj[0].label_para[2], "%3d.%02d", sys_cur_para.input_vol * sys_cur_para.input_cur / 1000000, \
                          ((sys_cur_para.input_vol * sys_cur_para.input_cur) % 1000000) / 10000);
    lv_label_set_text_fmt(ui_status.para_obj[1].label_para[0], "%3d.%02d", sys_cur_para.output_vol / 1000, (sys_cur_para.output_vol/ 10) % 100);
    lv_label_set_text_fmt(ui_status.para_obj[1].label_para[1], "%3d.%02d", sys_cur_para.output_cur / 1000, (sys_cur_para.output_cur / 10) % 100);
    lv_label_set_text_fmt(ui_status.para_obj[1].label_para[2], "%3d.%02d", sys_cur_para.output_cur * sys_cur_para.output_vol / 1000000, \
                          ((sys_cur_para.output_cur * sys_cur_para.output_vol) % 1000000) / 10000);//输出的功率
    
    lv_label_set_text_fmt(ui_status.label_effi, "%d.%d%c", sys_cur_para.output_cur * sys_cur_para.output_vol * 100 / (sys_cur_para.input_cur * sys_cur_para.input_vol),\
                          ((sys_cur_para.output_cur * sys_cur_para.output_vol * 1000 / (sys_cur_para.input_cur * sys_cur_para.input_vol)) / 100) % 10, '%');
    
    //超过5V输入
    if(sys_cur_para.input_vol > 5000)
    {
        lv_img_set_src(ui_status.para_obj[0].img_title, &input_en);
    }
    else
    {
        lv_img_set_src(ui_status.para_obj[0].img_title, &input_dis);
    }
    if(sys_cur_para.pow_out_en)
    {
        lv_img_set_src(ui_status.para_obj[1].img_title, &output_en);
    }
    else
    {
        lv_img_set_src(ui_status.para_obj[1].img_title, &output_dis);
    }
}

static void slider_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);

    if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
        lv_coord_t * s = lv_event_get_param(e);
        *s = LV_MAX(*s, 60);
    }
    else if(code == LV_EVENT_DRAW_PART_END)
    {
        lv_obj_draw_part_dsc_t * dsc = lv_event_get_param(e);
        if(dsc->part == LV_PART_KNOB && lv_obj_has_state(obj, LV_STATE_PRESSED)) 
        {
            char buf[8];
            lv_snprintf(buf, sizeof(buf), "%"LV_PRId32, lv_slider_get_value(obj));

            lv_point_t text_size;
            lv_txt_get_size(&text_size, buf, &lv_font_montserrat_14, 0, 0, LV_COORD_MAX, LV_TEXT_FLAG_NONE);

            lv_area_t txt_area;
            txt_area.x1 = dsc->draw_area->x1 + lv_area_get_width(dsc->draw_area) / 2 - text_size.x / 2;
            txt_area.x2 = txt_area.x1 + text_size.x;
            txt_area.y2 = dsc->draw_area->y1 - 10;
            txt_area.y1 = txt_area.y2 - text_size.y;

            lv_area_t bg_area;
            bg_area.x1 = txt_area.x1 - LV_DPX(8);
            bg_area.x2 = txt_area.x2 + LV_DPX(8);
            bg_area.y1 = txt_area.y1 - LV_DPX(8);
            bg_area.y2 = txt_area.y2 + LV_DPX(8);

            lv_draw_rect_dsc_t rect_dsc;
            lv_draw_rect_dsc_init(&rect_dsc);
            rect_dsc.bg_color = lv_palette_darken(LV_PALETTE_GREY, 3);
            rect_dsc.radius = LV_DPX(5);
            lv_draw_rect(dsc->draw_ctx, &rect_dsc, &bg_area);

            lv_draw_label_dsc_t label_dsc;
            lv_draw_label_dsc_init(&label_dsc);
            label_dsc.color = lv_color_white();
            label_dsc.font = &lv_font_montserrat_14;
            lv_draw_label(dsc->draw_ctx, &label_dsc, &txt_area, buf, NULL);
        }
    }
}

static void ui_event_cb(lv_event_t * e)
{
    uint8_t i;
    lv_obj_t * obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_SHORT_CLICKED)
    {
        //输出使能按键
        if(obj == ui_status.btn_pow_en)
        {
            if(lv_obj_has_state(obj, LV_STATE_CHECKED))
            {
                sys_cur_para.pow_out_en = 1;
                lv_label_set_text(ui_status.label_btn_pow_en, "ON");
            }
            else
            {
                sys_cur_para.pow_out_en = 0;
                lv_label_set_text(ui_status.label_btn_pow_en, "OFF");
            }
        }
        //设置按键单击
        else if(obj == ui_status.btn_set)
        {
            if(ui_status.calib_create_flag == 0)
            {
                ui_status.calib_create_flag = 1;
                calib_sel_ui_init(lv_scr_act());
            }
        }
    }
    else if(code == LV_EVENT_LONG_PRESSED)
    {
        if(obj == ui_status.btn_set)
        {
            if(ui_status.setting_create_flag == 0)
            {
                ui_status.setting_create_flag = 1;
                ui_setting_init(lv_scr_act());
            }
        }
    }
    else if(code == LV_EVENT_VALUE_CHANGED)
    {
        if(obj == ui_status.drop_vol_sel)
        {
            uint16_t sel = lv_dropdown_get_selected(obj);
            sys_cur_para.fast_vol_sel = sel;
            sys_cur_para.user_set.vol_set = sys_cur_para.user_set.dac_fast_vol[sel];
            sys_cur_para.para_save_flag = 1;
        }
    }
}

bool test_open_logo_file(void) 
{
    lv_fs_file_t f;
    lv_fs_res_t res;
    
    /* 尝试打开1:/logo/001.jpg文件 */
    res = lv_fs_open(&f, "1:/logo/001.jpg", LV_FS_MODE_RD);
    
    if(res == LV_FS_RES_OK) {
        printf("open file ok: 1:/logo/001.jpg\n");
        lv_fs_close(&f);
        return true;
    } else {
        printf("open file error: 1:/logo/001.jpg, code: %d\n", res);
        return false;
    }
}

void main_page_init(void)
{
    test_open_logo_file();
    
    g = lv_group_get_default();
    if(g == NULL) {
        g = lv_group_create();
        lv_group_set_default(g);
    }

    lv_indev_t * cur_drv = NULL;
    for(;;) 
    {
        cur_drv = lv_indev_get_next(cur_drv);
        if(!cur_drv) {
            break;
        }
        if(cur_drv->driver->type == LV_INDEV_TYPE_KEYPAD) {
            lv_indev_set_group(cur_drv, g);
        }
        if(cur_drv->driver->type == LV_INDEV_TYPE_ENCODER) {
            lv_indev_set_group(cur_drv, g);
        }
        if(cur_drv->driver->type == LV_INDEV_TYPE_BUTTON) {
            lv_indev_set_group(cur_drv, g);
        }
    }

    ui_status.calib_create_flag = 0;
    
    lv_obj_t * parent = lv_scr_act();
    ui_status.main_bg = lv_img_create(parent);
    //lv_img_set_src(ui_status.main_bg, &pic_bg);
    lv_obj_set_size(ui_status.main_bg, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_pad_all(ui_status.main_bg, 0, 0);
    lv_obj_align(ui_status.main_bg,LV_ALIGN_CENTER, 0, 0);
    status_page_ui_init(ui_status.main_bg);

    lv_task_main = lv_timer_create(&lv_task_main_handler, 200, 0);
}

const char * label_para_title_text[3][2] = 
{
    {"IN:", "OUT:"},
    {"输入:", "输出:"},
    {"輸入:", "輸出:"}
};
const char * label_para_uint_text[3] = {"V", "A", "W"};
const void * img_para_title_logo_src_tab[2] = {&input_dis, &output_dis};

void status_page_ui_init(lv_obj_t * parent)
{
    int i, j;
    
    for(i=0;i<2;i++)
    {
        ui_status.para_obj[i].cont_para = lv_obj_create(parent);
        lv_obj_set_style_pad_all(ui_status.para_obj[i].cont_para, 0, 0);
        lv_obj_set_style_pad_gap(ui_status.para_obj[i].cont_para, 0, 0);
        lv_obj_set_style_radius(ui_status.para_obj[i].cont_para, 5, 0);
        lv_obj_clear_flag(ui_status.para_obj[i].cont_para, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_size(ui_status.para_obj[i].cont_para, 157, 142);
        lv_obj_set_style_border_width(ui_status.para_obj[i].cont_para, 2, 0);
        lv_obj_set_style_border_color(ui_status.para_obj[i].cont_para, lv_color_black(), 0);
        lv_obj_set_style_bg_color(ui_status.para_obj[i].cont_para, lv_palette_lighten(LV_PALETTE_BLUE, 2), 0);
        
        ui_status.para_obj[i].label_title = lv_label_create(ui_status.para_obj[i].cont_para);
        lv_obj_set_style_text_font(ui_status.para_obj[i].label_title, &font_siyuanheitim_16, 0);
        lv_label_set_text(ui_status.para_obj[i].label_title, label_para_title_text[sys_cur_para.user_set.language][i]);
        lv_obj_align(ui_status.para_obj[i].label_title, LV_ALIGN_TOP_LEFT, 2, 2);
        
        ui_status.para_obj[i].img_title = lv_img_create(ui_status.para_obj[i].cont_para);
        lv_img_set_src(ui_status.para_obj[i].img_title, img_para_title_logo_src_tab[i]);
        lv_obj_align_to(ui_status.para_obj[i].img_title, ui_status.para_obj[i].label_title, LV_ALIGN_OUT_RIGHT_MID, 12, 0);
        
        for(j=0;j<3;j++)
        {
            ui_status.para_obj[i].label_para[j] = lv_label_create(ui_status.para_obj[i].cont_para);
            lv_obj_set_style_text_font(ui_status.para_obj[i].label_para[j], &font_zhankuqingke_28, 0);
            lv_obj_set_style_text_color(ui_status.para_obj[i].label_para[j], lv_color_black(), 0);
            if(j == 0)
                lv_obj_align_to(ui_status.para_obj[i].label_para[j], ui_status.para_obj[i].label_title, LV_ALIGN_OUT_BOTTOM_LEFT, 0, -3);
            else
                lv_obj_align_to(ui_status.para_obj[i].label_para[j], ui_status.para_obj[i].label_para[j-1], LV_ALIGN_OUT_BOTTOM_LEFT, 0, -10);
            lv_label_set_text(ui_status.para_obj[i].label_para[j], "000.00");
            lv_obj_set_style_text_align(ui_status.para_obj[i].label_para[j], LV_TEXT_ALIGN_RIGHT, 0);
            
            ui_status.para_obj[i].label_para_uint[j] = lv_label_create(ui_status.para_obj[i].cont_para);
            lv_obj_set_style_text_font(ui_status.para_obj[i].label_para_uint[j], &font_zhankuqingke_28, 0);
            lv_obj_set_style_text_color(ui_status.para_obj[i].label_para_uint[j], lv_color_black(), 0);
            lv_obj_align_to(ui_status.para_obj[i].label_para_uint[j], ui_status.para_obj[i].label_para[j], LV_ALIGN_OUT_RIGHT_MID, 0, 0);
            lv_label_set_text(ui_status.para_obj[i].label_para_uint[j], label_para_uint_text[j]);
        }
        
        lv_obj_set_style_text_color(ui_status.para_obj[i].label_para[0], lv_palette_main(LV_PALETTE_RED), 0);
        lv_obj_set_style_text_color(ui_status.para_obj[i].label_para[1], lv_palette_main(LV_PALETTE_GREEN), 0);
        lv_obj_set_style_text_color(ui_status.para_obj[i].label_para[2], lv_palette_main(LV_PALETTE_YELLOW), 0);
        
        lv_obj_set_style_text_color(ui_status.para_obj[i].label_para_uint[0], lv_palette_main(LV_PALETTE_RED), 0);
        lv_obj_set_style_text_color(ui_status.para_obj[i].label_para_uint[1], lv_palette_main(LV_PALETTE_GREEN), 0);
        lv_obj_set_style_text_color(ui_status.para_obj[i].label_para_uint[2], lv_palette_main(LV_PALETTE_YELLOW), 0);
    }
    
    lv_obj_align(ui_status.para_obj[0].cont_para, LV_ALIGN_TOP_LEFT, 2, 2);
    
    lv_obj_set_style_bg_color(ui_status.para_obj[1].cont_para, lv_palette_lighten(LV_PALETTE_BLUE, 2), 0);
    lv_obj_align(ui_status.para_obj[1].cont_para, LV_ALIGN_TOP_RIGHT, -2, 2);
    
    ui_status.cont_ctr = lv_obj_create(parent);
    lv_obj_set_style_pad_all(ui_status.cont_ctr, 0, 0);
    lv_obj_set_style_pad_gap(ui_status.cont_ctr, 0, 0);
    lv_obj_set_style_radius(ui_status.cont_ctr, 5, 0);
    lv_obj_clear_flag(ui_status.cont_ctr, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(ui_status.cont_ctr, 316, 92);
    lv_obj_align(ui_status.cont_ctr, LV_ALIGN_BOTTOM_LEFT, 2, -2);
    lv_obj_set_style_border_width(ui_status.cont_ctr, 2, 0);
    lv_obj_set_style_border_color(ui_status.cont_ctr, lv_color_black(), 0);
    lv_obj_set_style_bg_color(ui_status.cont_ctr, lv_palette_lighten(LV_PALETTE_BLUE, 2), 0);
    
    ui_status.label_run_time = lv_label_create(ui_status.cont_ctr);
    lv_label_set_text_fmt(ui_status.label_run_time,"%d:%02d",(lv_tick_get()/60000)%100,(lv_tick_get()/1000)%60);
    lv_obj_set_style_text_font(ui_status.label_run_time, &font_siyuanheitim_16, 0);
    lv_obj_align(ui_status.label_run_time, LV_ALIGN_BOTTOM_LEFT, 2, 0);
    
//    ui_status.slider = lv_slider_create(ui_status.cont_ctr);
//    lv_slider_set_range(ui_status.slider, 0, 4095);
//    lv_slider_set_value(ui_status.slider, 0, LV_ANIM_ON);
//    lv_obj_add_event_cb(ui_status.slider, ui_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
//    lv_obj_align(ui_status.slider, LV_ALIGN_BOTTOM_MID, 0, -2);
    
    ui_status.btn_pow_en = lv_switch_create(ui_status.cont_ctr);
    lv_obj_set_size(ui_status.btn_pow_en, 100, 42);
    //lv_obj_add_flag(ui_status.btn_pow_en, LV_OBJ_FLAG_CHECKABLE);
    //lv_obj_set_style_bg_color(ui_status.btn_pow_en, lv_palette_lighten(LV_PALETTE_GREEN, 2), LV_STATE_CHECKED);
    //lv_obj_set_style_bg_color(ui_status.btn_pow_en, lv_palette_lighten(LV_PALETTE_RED, 2), LV_STATE_DEFAULT);
    lv_obj_add_event_cb(ui_status.btn_pow_en, ui_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_align(ui_status.btn_pow_en, LV_ALIGN_BOTTOM_RIGHT, -5, -5);
    
    ui_status.label_btn_pow_en = lv_label_create(ui_status.btn_pow_en);
    lv_obj_set_style_text_font(ui_status.label_btn_pow_en, &font_zhankuqingke_28, 0);
    lv_obj_set_style_text_color(ui_status.label_btn_pow_en, lv_color_black(), 0);
    lv_obj_center(ui_status.label_btn_pow_en);
    lv_label_set_text(ui_status.label_btn_pow_en, "OFF");
    
    ui_status.drop_vol_sel = lv_dropdown_create(ui_status.cont_ctr);
    //                                               608   624   663   689 705   722   743 799 852 10171178 1342 1505 1615 1831 2048 2265 2481 31393792
    lv_dropdown_set_options(ui_status.drop_vol_sel, "1.5V\n1.8V\n2.5V\n3V\n3.3V\n3.6V\n4V\n5V\n6V\n9V\n12V\n15V\n18V\n20V\n24V\n28V\n32V\n36V\n48V\n60V\nMAX");
    lv_dropdown_set_selected(ui_status.drop_vol_sel, sys_cur_para.fast_vol_sel);
    lv_dropdown_set_symbol(ui_status.drop_vol_sel, &drop_symbol);
    lv_obj_set_style_text_font(ui_status.drop_vol_sel, &font_siyuanheitim_16, LV_PART_MAIN);
    lv_obj_set_style_text_font(lv_dropdown_get_list(ui_status.drop_vol_sel), &font_siyuanheitim_16, LV_PART_MAIN);
    lv_obj_set_style_pad_all(ui_status.drop_vol_sel, 2, 0);
    lv_obj_add_event_cb(ui_status.drop_vol_sel, ui_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_align(ui_status.drop_vol_sel, LV_ALIGN_TOP_LEFT, 2, 2);
    
    ui_status.label_effi = lv_label_create(ui_status.cont_ctr);
    lv_label_set_text_fmt(ui_status.label_effi,"EFFI:100%c",'%');
    lv_obj_set_style_text_font(ui_status.label_effi, &font_siyuanheitim_16, 0);
    lv_obj_align_to(ui_status.label_effi, ui_status.drop_vol_sel, LV_ALIGN_OUT_RIGHT_MID, 30, 0);
    
    ui_status.btn_set = lv_btn_create(ui_status.cont_ctr);
    lv_obj_set_size(ui_status.btn_set, 40, 40);
    lv_obj_set_style_bg_img_src(ui_status.btn_set, &logo_setting, 0);
    lv_obj_align_to(ui_status.btn_set, ui_status.btn_pow_en, LV_ALIGN_OUT_LEFT_MID, -8, 0);
    lv_obj_add_event_cb(ui_status.btn_set, ui_event_cb, LV_EVENT_ALL, NULL);
}
